<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Packet_Model extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('entity/shop_packet_entity', 'Shop_Packet_Entity');
    }

    public function getAvailablePacket($select = array(), $options = array())
    {
        $this->load->model('entity/packet_entity', 'Packet_Entity');
        if (count($select) > 0) {
            $this->db->select(implode(',', $select));
        } else {
            $this->db->select('a.id, a.title, a.product_count, a.price');
        }
        $this->db->from($this->table_packet_name . ' AS a');
        if (count($options) > 0) {
            $this->db->where(implode(',', $options));
        } else {
            $this->db->where('a.deleted_at', null);
        }
        return $this->db->get()->result('Packet_Entity');
    }

    public function getShopPacket($shop_id, $options = array(), $select = array())
    {
        $this->db->select('a.id, a.upload_time, a.active_flg, a.created_at, a.updated_at, b.title, b.product_count');
        $this->db->from($this->table_shop_packet_name . ' AS a');
        $this->db->join($this->table_packet_name . ' b', 'a.packet_id=b.id');
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('a.shop_id', $shop_id);
        $this->db->order_by('a.created_at', 'desc');
        $this->db->order_by('a.updated_at', 'desc');
        return $this->db->get()->result('Shop_Packet_Entity');
    }

    public function addPacket($packet)
    {
        $this->db->trans_begin();
        $this->db->insert($this->table_packet_name, $packet);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    public function updatePacket($id, $packet)
    {
        $this->db->trans_begin();
        $this->db->where('id', $id);
        $this->db->update($this->table_packet_name, $packet);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    public function getRegisterPacket($active_flg)
    {
        $this->db->select('a.id, a.shop_id, a.packet_id, a.active_flg, a.created_at, b.title as packet_title, c.title as shop_title');
        $this->db->from($this->table_shop_packet_name . ' AS a');
        $this->db->join($this->table_packet_name . ' b', 'a.packet_id=b.id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        if ($active_flg != null) {
            $this->db->where('a.active_flg', $active_flg);
        }
        $this->db->order_by('a.created_at', 'asc');
        $this->db->order_by('a.updated_at', 'asc');
        return $this->db->get()->result('Shop_Packet_Entity');
    }

    public function buyPacket($packet)
    {
        $this->db->trans_begin();
        $this->db->insert($this->table_shop_packet_name, $packet);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    public function updateRegisterPacket($id, $packet)
    {
        $this->db->trans_begin();
        $this->db->where('id', $id);
        $this->db->update($this->table_shop_packet_name, $packet);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }
}
?>